package com.travelmood.controller;
import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.http.HttpEntity;
        import org.springframework.http.HttpHeaders;
        import org.springframework.http.MediaType;
        import org.springframework.stereotype.Service;
        import org.springframework.web.client.RestTemplate;
        import org.springframework.web.multipart.MultipartFile;

        import java.util.Base64;
        import java.util.HashMap;
        import java.util.Map;
        import java.util.logging.Logger;

@Service
public class FastApiService {

    private static final Logger logger = Logger.getLogger(FastApiService.class.getName());

    @Autowired
    private RestTemplate restTemplate;

    public String submitImage(MultipartFile file, String style) {
        try {
            String base64Image = Base64.getEncoder().encodeToString(file.getBytes());
            String initImages = "data:image/" + file.getContentType().split("/")[1] + ";base64," + base64Image;

            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("file", initImages);
            requestBody.put("style", style);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);

            String fastApiUrl = "http://localhost:8000/submit/";
            logger.info("Sending request to FastAPI: " + fastApiUrl);
            logger.info("Request body: " + requestBody);

            String response = restTemplate.postForObject(fastApiUrl, requestEntity, String.class);
            logger.info("Response from FastAPI: " + response);
            return response;
        } catch (Exception e) {
            logger.severe("Failed to call FastAPI: " + e.getMessage());
            throw new RuntimeException("Failed to call FastAPI", e);
        }
    }
}
